import wx
import os	
import dropbox
import googledoc

#define file drop target class
class FileDropTarget(wx.FileDropTarget):
	#this object implements drop target function
	def __init__(self, obj, doc, dropbox):
		wx.FileDropTarget.__init__(self)
		self.obj = obj
		self.doc =doc	
		self.dropbox=dropbox
	def OnDropFiles(self, x,y, filenames):
		#implement file drop
#		self.obj.SetInsertionPointEnd()
#		self.obj.WriteText("%d file(s) dropped:\n" %(len(filenames)))
		for file in filenames:
#			self.obj.WriteText(file + '\n')
			self.doc.upload(file)
#			self.dropbox.upload(file)

#login dialog
class LoginDialog(wx.Dialog):
	def __init__(self, parent, ID, title, size=wx.DefaultSize, pos=wx.DefaultPosition,style=wx.DEFAULT_DIALOG_STYLE,useMetal=False):

		# Instead of calling wx.Dialog.__init__ we precreate the dialog
		# so we can set an extra style that must be set before
		# creation, and then we create the GUI object using the Create
		# method.
		pre = wx.PreDialog()
		pre.SetExtraStyle(wx.DIALOG_EX_CONTEXTHELP)
		pre.Create(parent, ID, title, pos, size, style)

		# This next step is the most important, it turns this Python
		# object into the real wrapper of the dialog (instead of pre)
		# as far as the wxPython extension is concerned.
		self.PostCreate(pre)

		# This extra style can be set after the UI object has been created.
		if 'wxMac' in wx.PlatformInfo and useMetal:
			self.SetExtraStyle(wx.DIALOG_EX_METAL)


		# Now continue with the normal construction of the dialog
		# contents
		sizer = wx.BoxSizer(wx.VERTICAL)
		box = wx.BoxSizer(wx.HORIZONTAL)

		#user name field
		label = wx.StaticText(self, -1, "username:")
		box.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)

		usr_TextCtrl = wx.TextCtrl(self, -1, "", size=(180,-1))
		self.Bind(wx.EVT_TEXT, self.UEvtText, usr_TextCtrl)
		box.Add(usr_TextCtrl, 1, wx.ALIGN_CENTRE|wx.ALL, 5)

		sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)

		#password field
		box = wx.BoxSizer(wx.HORIZONTAL)

		label = wx.StaticText(self, -1, "password:")
		box.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)

		pwd_TextCtrl = wx.TextCtrl(self, -1, "", size=(180,-1), style=wx.TE_PASSWORD)
		self.Bind(wx.EVT_TEXT, self.PEvtText, pwd_TextCtrl)
		box.Add(pwd_TextCtrl, 1, wx.ALIGN_CENTRE|wx.ALL, 5)

		sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)

		# sepereate line
		line = wx.StaticLine(self, -1, size=(20,-1), style=wx.LI_HORIZONTAL)
		sizer.Add(line, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.RIGHT|wx.TOP, 5)

		btnsizer = wx.StdDialogButtonSizer()
		
		if wx.Platform != "__WXMSW__":
			btn = wx.ContextHelpButton(self)
			btnsizer.AddButton(btn)
		
		btn = wx.Button(self, wx.ID_OK)
	#	self.Bind(wx.EVT_BUTTON, self.OnButton, self.btn)
		btn.SetDefault()
		btnsizer.AddButton(btn)

		btn = wx.Button(self, wx.ID_CANCEL)
		btnsizer.AddButton(btn)
		btnsizer.Realize()

		sizer.Add(btnsizer, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)

		self.SetSizer(sizer)
		sizer.Fit(self)

	#pwd event
	def PEvtText(self, event):
		self.pwd= event.GetString()

	#pwd username event 
	def UEvtText(self, event):
		self.usr= event.GetString()
#frame size
width=150
height=160
class MainWindow(wx.Frame):
	def __init__(self, parent,id,title):
		wx.Frame.__init__(self,parent,-4, title, size = (width,height),style=wx.MINIMIZE_BOX | wx.MAXIMIZE_BOX | wx.RESIZE_BORDER | wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX | wx.CLIP_CHILDREN )
		self.SetBackgroundColour(wx.WHITE)
		self.SetMaxSize(wx.Size(width,height))
		self.SetMinSize(wx.Size(width,height))
		wx.EVT_MENU(self, wx.ID_EXIT, self.CloseWindow)
		
		self.panel = p = wx.Panel(self)
	
		self.doc = googledoc.GoogleDoc()
		self.dropbox= dropbox.DropBox()
		#add pic
		png = wx.Image('upload.png', wx.BITMAP_TYPE_PNG).ConvertToBitmap()
		wx.StaticBitmap(p, -1, png )

		#popup menu
		p.Bind(wx.EVT_CONTEXT_MENU, self.OnContextMenu)

		dt=FileDropTarget(self.panel, self.doc, self.dropbox)
		self.panel.SetDropTarget(dt)
		self.Show(True)

	def OnContextMenu(self, event):
		# only do this part the first time so the events are only bound once
		#
		# Yet another anternate way to do IDs. Some prefer them up top to
		# avoid clutter, some prefer them close to the object of interest
		# for clarity. 

		self.loginItem = wx.NewId()
		self.loginItem_g = wx.NewId()
		self.loginItem_d = wx.NewId()
		self.exitItem = wx.NewId()

		self.Bind(wx.EVT_MENU, self.loginAction, id=self.loginItem)
		self.Bind(wx.EVT_MENU, self.loginAction_g, id=self.loginItem_g)
		self.Bind(wx.EVT_MENU, self.loginAction_d, id=self.loginItem_d)
		self.Bind(wx.EVT_MENU, self.CloseWindow, id=self.exitItem)

		# make a menu
		menu = wx.Menu()
		#make a submenu
		select_account= wx.Menu()
		select_account.Append(self.loginItem_g, "google doc")
		select_account.Append(self.loginItem_d, "dropbox")
		menu.AppendMenu(self.loginItem, "Login", select_account)
		# add some other items
		menu.Append(self.exitItem, "Exit")
		
		# Popup the menu.  If an item is selected then its handler
		# will be called before PopupMenu returns.
		self.PopupMenu(menu)
		menu.Destroy()


	def createLoginDialog(self, title):
		useMetal = False
		if 'wxMac' in wx.PlatformInfo:
			useMetal = self.cb.IsChecked()
			
		dlg = LoginDialog(self, -1, title, size=(800, 200),
						 #style=wx.CAPTION | wx.SYSTEM_MENU | wx.THICK_FRAME,
						 style=wx.DEFAULT_DIALOG_STYLE, # & ~wx.CLOSE_BOX,
						 useMetal=useMetal,
						 )
		dlg.CenterOnScreen()

		# this does not return until the dialog is closed.
		val = dlg.ShowModal()
		

		if val == wx.ID_OK:
			self.loginAccount(title,dlg.usr, dlg.pwd)
		else:
			pass
		dlg.Destroy()
	#login event
	def loginAccount(self, title, usr, pwd):
		if title =='google doc login':
			self.doc.login(usr,pwd)
		elif title == 'dropbox login':
			self.dropbox.login(usr,pwd)
			self.dropbox.account_info()
	def loginAction(self, event):
		pass
	def loginAction_g(self, event):
		self.createLoginDialog('google doc login')
	def loginAction_d(self, event):
		self.createLoginDialog('dropbox login')

	def CloseWindow(self, event):
		self.Close()
	
	def OnDragInit(self, event):
		tdo = wx.PyTextDataObject(self.text.GetStringSelection())
		tds = wx.DropSource(self.text)
		tds.SetData(tdo)
		tds.DoDragDrop(True)

	




